package servlet;

import model.User;
import service.FavoriteService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加收藏的Servlet
 */
@WebServlet(name = "favorite_add", urlPatterns = "/favorite_add")
public class FavoriteAddServlet extends HttpServlet {
    private FavoriteService favoriteService = new FavoriteService();

    /**
     * 处理收藏商品的请求
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取商品ID
        int goods_id = Integer.parseInt(request.getParameter("goodsid"));
        
        // 从session中获取用户信息
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            // 用户未登录，跳转到登录页面
            response.sendRedirect("user_login.jsp");
            return;
        }
        
        // 添加收藏
        boolean success = favoriteService.addFavorite(user.getId(), goods_id);
        
        // 跳转回商品详情页
        response.sendRedirect("goods_detail?id=" + goods_id);
    }
} 